package com.skyable.common.aop;

import com.alibaba.fastjson.JSONObject;
import com.skyable.common.annotation.OperationLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author GaoYuan
 * @date 2020/12/10 15:19
 * @desc 操作日志AOP
 */
@Aspect
@Component
@Slf4j
public class OperateLogAcpect {

    /**
     * 定义切点
     * 此处的切点是注解的方式，也可以用包名的方式达到相同的效果
     */
    @Pointcut("@annotation(com.skyable.common.annotation.OperationLog)")
    public void operationLog(){}


    @AfterReturning(returning = "ret",pointcut = "operationLog()")
    public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        OperationLog operationLog = signature.getMethod().getAnnotation(OperationLog.class);
        log.info(JSONObject.toJSONString(operationLog));
    }

}
